package com.examsystem.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.examsystem.common.ResultJson;
import com.examsystem.common.StatusAndMsg;
import com.examsystem.dao.exame_subjecttypeMapper;
import com.examsystem.entity.exame_subjecttype;
import com.examsystem.entity.exame_subjecttypeExample;
import com.examsystem.entity.exame_subjecttypeExample.Criteria;
import com.examsystem.service.SubjectTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SubjectTypeServiceImpl implements SubjectTypeService {

	@Autowired
	private exame_subjecttypeMapper subjectTypeMapper;

	/**
	 * 根据页码，和每页大小查询
	 */
	@Override
	public ResultJson selectSubjectTypeList(Integer page, Integer pageSize) {
		ResultJson result = null;
		// 操作数据库
		PageHelper.startPage(page, pageSize);
		List<exame_subjecttype> list = subjectTypeMapper.selectByExample(null);
		if (null == list || list.size() == 0) {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		} else {
			PageInfo<exame_subjecttype> pageInfo = new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}
		// 返回数据
		return result;
	}

	/**
	 * 模糊查询（排序，名称，主键）
	 */
	@Override
	public ResultJson selectByExamp(exame_subjecttype subjecttype, String Sort, Integer page, Integer pageSize) {
		ResultJson result = null;
		// 构造查询条件
		exame_subjecttypeExample example = new exame_subjecttypeExample();
		if ("DESC".equals(Sort) || "ASC".equals(Sort)) {
			example.setOrderByClause("createTime" + " " + Sort);
		}
		Criteria criteria = example.createCriteria();
		if (null != subjecttype.getStid()) {
			// 根据主键查询（唯一）
			criteria.andStidEqualTo(subjecttype.getStid());
		} else {
			if (StringUtils.isNotBlank(subjecttype.getStname())) {
				// 根据科目名
				criteria.andStnameLike("%"+subjecttype.getStname()+"%");
			}
		}
		// 操作数据库
		PageHelper.startPage(page, pageSize);
		List<exame_subjecttype> list = subjectTypeMapper.selectByExample(example);
		if (null == list || list.size() == 0) {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		} else {
			PageInfo<exame_subjecttype> pageInfo = new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}
		// 返回数据
		return result;
	}

	/**
	 * 根据id修改记录
	 */
	@Override
	public ResultJson updateByKey(exame_subjecttype subjecttype) {
		ResultJson result = null;
		// 判断主键
		if (null == subjecttype.getStid()) {
			result = new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
			return result;
		}
		// 操作数据库
		int i = subjectTypeMapper.updateByPrimaryKeySelective(subjecttype);
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}

	/**
	 * 根据id删除记录(支持批量删除)
	 */
	@Override
	public ResultJson deleteByKey(List<Integer> ids) {
		ResultJson result = null;
		exame_subjecttypeExample example=new exame_subjecttypeExample();
		Criteria criteria = example.createCriteria();
		criteria.andStidIn(ids);
		int i = subjectTypeMapper.deleteByExample(example);
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		// 操作数据库
		return result;
	}

	//插入记录
	@Override
	public ResultJson insertInfo(exame_subjecttype subjecttype) {
		ResultJson result = null;
		//补全信息
		subjecttype.setCreatetime(new Date());
		subjecttype.setUpdatetime(new Date());
		//操作数据库
		int i = subjectTypeMapper.insertSelective(subjecttype);
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		// 操作数据库
		return result;
	}

}
